package com.wr.controllers;

import com.wr.pojos.FavoriteEntity;
import com.wr.pojos.PersonalEntity;
import com.wr.pojos.UserEntity;
import com.wr.pojos.UserImageEntity;
import com.wr.services.FavoriteService;
import com.wr.services.PersonalService;
import com.wr.services.UserImageService;
import com.wr.services.UserService;
import com.wr.utils.ConfigUtils;
import com.wr.utils.EhcacheUtils;
import com.wr.utils.UpLoadFileUtil;
import com.wr.utils.Utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/personal")
public class PersonalController {

    @Resource
    private PersonalService personalService;

    @Resource
    private FavoriteService favoriteService;

    @Resource
    private UserImageService imageService;

    @Resource
    private UserService userService;

    /**
     * 进入个人信息页面
     * @return
     */
    @RequestMapping("/toPersonalInfo")
    public String toform(Model model){
        Subject subject = SecurityUtils.getSubject();
        UserEntity userEntity = (UserEntity) subject.getPrincipal();
        PersonalEntity personalEntity = personalService.getPersonalInfo(userEntity.getId());
        List<FavoriteEntity> favoriteList = favoriteService.selectFavoriteList();
        model.addAttribute("personal",personalEntity);
        model.addAttribute("favoriteList",favoriteList);
        return "/layui/form";
    }

    /**
     * 保存个人信息
     * @param personalEntity
     * @return
     */
    @RequestMapping("/savePersonalInfo")
    public String savePersonalInfo(PersonalEntity personalEntity){
        Subject subject = SecurityUtils.getSubject();
        UserEntity user = (UserEntity) subject.getPrincipal();

        //id为空表示新增
        if(personalEntity.getId()==0){
            personalEntity.setUserId(user.getId());
            personalEntity.setAddTime(Utils.formatLongDate(new Date()));
            personalService.insertPersonalInfo(personalEntity);
        }else{      //id不为空表示更新
            personalEntity.setUpdateTime(Utils.formatLongDate(new Date()));
            personalService.updatePersonalInfo(personalEntity);
        }

        return "redirect:toPersonalInfo.do";
    }

    /**
     * 上传文件
     * @param request   请求
     * @throws IOException IO异常
     * @return 返回值
     */
    @RequestMapping("/uploadHeadImg")
    @ResponseBody
    public Map<String,Object> uploadHeadImg(HttpServletRequest request) throws IOException{
        UserEntity userEntity = (UserEntity) EhcacheUtils.get("myCache","userEntity");
        Map<String,Object> map = new HashMap<>();
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartRequest.getFile("headImg");
        String path = "headImg/";
        String contentType = multipartFile.getContentType().substring(multipartFile.getContentType().indexOf("/")+1,multipartFile.getContentType().length());
        byte[] buffer = multipartFile.getBytes();
        FileOutputStream fo;
        fo = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\手机相册\\"+ path+userEntity.getMobile()+"."+contentType);
        fo.write(buffer);
        fo.flush();
        fo.close();

        //注释内容为ftp上传模式
        /*CommonsMultipartFile cf = (CommonsMultipartFile) multipartFile;
        InputStream inputStream = cf.getInputStream();
        UpLoadFileUtil.uploadFile(ConfigUtils.FTPURL,ConfigUtils.FTPPORT,ConfigUtils.FTPUSERNAME,
                ConfigUtils.FTPPASSWORD,path,userEntity.getMobile()+"."+contentType,inputStream);*/
        PersonalEntity personalEntity = personalService.getPersonalInfo(userEntity.getId());
        personalEntity.setHeadImg(path+userEntity.getMobile()+"."+contentType);
        personalService.updatePersonalInfo(personalEntity);

        userEntity = userService.authentication(userEntity);
        EhcacheUtils.put("myCache","userEntity",userEntity);
        map.put("result","success");
        return map;
    }
}
